package net.com.hydroPK;

import net.com.hydroPK.model.Player;
import net.com.hydroPK.model.items.PlayerItems;

/**
 * Created by Roflmaodude1
 * Purpose: To create a shop system that doesnt fuck up and DC
 * Date created: 19/12/09
 * Time started: 22:48
 */
public class ShopHandler {
    Player p;
    PlayerItems pi = new PlayerItems();
    public int maxItems = 40;
    //public int[] items = new int[maxItems];
    //public int[] itemsN = new int[maxItems];
    //public int[] maxItemAmount = new int[maxItems];
    public boolean generalStore = false;
    public boolean buysBack = false;
    public long lastRestock = System.currentTimeMillis();
    int shopid = 0;
    public boolean mainstock = false;

    public ShopHandler(Player player) {
        this.p = player;
    }

    public void process(Player p) {
        if (System.currentTimeMillis() - lastRestock >= 60000) {
            clearSlots(p);
            for (int i = 0; i < p.shopItems.length; i++) {
                if (p.shopItemsN[i] < p.maxItemAmount[i]) p.shopItemsN[i]++;
            }
            lastRestock = System.currentTimeMillis();
        }
    }

    public void sell(Player p, int item, int amnt) {
        PlayerItems pi = new PlayerItems();
        boolean shopShouldBuy = false;
        if (generalStore || (buysBack && isItemOnShop(p, item))) {
            shopShouldBuy = true;
        }
        if (!shopShouldBuy) {
            p.message( "You cannot sell this item to this shop.");
            return;
        }
        if (item == 995) {
            p.message( "You can't sell coins to a shop");
            return;
        }
        if (item == 146 || item == 3025 || item == 158 || item == 170 || item == 140 || item == 164 || item == 145 || item == 3024 || item == 157 || item == 169 || item == 139 || item == 163) {
            p.message( "You can not sell the pots from the ::pots command.");
            return;
        }
        if (item == 555 || item == 556 || item == 557 || item == 558 || item == 559 || item == 560 | item == 9075 || item == 561 || item == 562 || item == 563 || item == 565 || item == 566 || item == 553 || item == 567) {
            p.message( "You can not sell the runes from any rune commands.");
            return;
        }
        if (item == 8839 || item == 8840 || item == 8841 || item == 8842 || item == 8843 || item == 8844 || item == 11663 || item == 11664 || item == 11665 || item == 10551 || item == 8850 || item == 7462 || item == 6570) {
            p.message( "You cannot sell this item.");
            return;
        }

        int free = findFreeSlot(p);
        if (!isItemOnShop(p, item) && generalStore) {
            if (free == -1) {
                p.message("This shop is full.");
                return;
            }
            p.shopItems[free] = item;
            p.shopItemsN[free] = 0;
        }
        int slot = findItemSlot(p, item);
    if(amnt<=pi.invItemCount(p,item))
    {
        if (itemStacks(item)) {
            p.shopItems[slot] = item;
            p.shopItemsN[slot] += amnt;
            pi.deleteItem(p, item, pi.getItemSlot(p, item), amnt);
                pi.addItem(p, 995, amnt*itemValue(item));
        } else {
            p.shopItems[slot] = item;
            for (int notused = amnt; notused > 0; notused--) {
                pi.deleteItem(p, item, pi.getItemSlot(p, item), 1);
                pi.addItem(p, 995, itemValue(item));
                p.shopItemsN[slot]++;
            }
        }
    }
    else
    {
        amnt = pi.invItemCount(p,item);
        if (itemStacks(item)) {
            p.shopItems[slot] = item;
            p.shopItemsN[slot] += amnt;
            pi.deleteItem(p, item, pi.getItemSlot(p, item), amnt);
                pi.addItem(p, 995, amnt*itemValue(item));
        } else {
            p.shopItems[slot] = item;
            for (int notused = amnt; notused > 0; notused--) {
                pi.deleteItem(p, item, pi.getItemSlot(p, item), 1);
                pi.addItem(p, 995, itemValue(item));
                p.shopItemsN[slot]++;
            }
        }
    }


        sendShopItems(p);
        sendPlayerInventory(p);
    }

    public void buy(Player p, int item, int amnt) {
        clearSlots(p);
        int slot = findItemSlot(p, item);
        if (slot == -1) {
            return;
        }
        if (p.shopItemsN[slot] < amnt) amnt = p.shopItemsN[slot];
        if (amnt == 0) {
            p.shopItems[slot] = -1;
            p.message("That item's stock has run out.");
            return;
        }
        if (itemStacks(item)) {
            if (pi.freeSlotCount(p) < 1) {
                p.message("Not enough space on inventory");
                return;
            }
            if (pi.invItemCount(p, 995) < (amnt * itemValue(item))) {
                p.message("Not enough coins to buy that many.");
                return;
            }
            pi.deleteItem(p, 995, pi.getItemSlot(p, 995), amnt * itemValue(item));
            pi.addItem(p, item, amnt);
            p.shopItemsN[slot] -= amnt;

        } else {
            for (int i = amnt; i > 0; i--) {
                int price = itemValue(item);
                clearSlots(p);
                if (pi.invItemCount(p, 995) < price) {
                    p.message("Not enough coins to buy that many.");
                    break;
                }
                if (pi.findInvSlot(p) == -1) {
                    p.message("Not enough space on inventory");
                    break;
                }
                if (p.shopItemsN[slot] < 1) {
                    clearSlots(p);
                    p.message("The shop has run out of stock from this item!");
                    break;
                }
                if (p.shopItemsN[slot] == 1) {
                    pi.deleteItem(p, 995, pi.getItemSlot(p, 995), price);
                    pi.addItem(p, item, 1);
                    p.shopItems[slot] = -1;

                    break;
                }
                p.shopItemsN[slot]--;

                pi.deleteItem(p, 995, pi.getItemSlot(p, 995), price);
                pi.addItem(p, item, 1);
                clearSlots(p);

            }
        }
        sendShopItems(p);
        sendPlayerInventory(p);
        clearSlots(p);
    }

    public void buystock(Player p, int item, int amnt) {
        if (itemStacks(item)) {
            if (pi.freeSlotCount(p) < 1) {
                p.message("Not enough space on inventory");
                return;
            }
            if (pi.invItemCount(p, 995) < (amnt * itemValue(item))) {
                p.message("Not enough coins to buy that many.");
                return;
            }
            pi.deleteItem(p, 995, pi.getItemSlot(p, 995), amnt * itemValue(item));
            pi.addItem(p, item, amnt);

        } else {
            for (int i = amnt; i > 0; i--) {
                int price = itemValue(item);
                if (pi.invItemCount(p, 995) < price) {
                    p.message("Not enough coins to buy that many.");
                    break;
                }
                if (pi.findInvSlot(p) == -1) {
                    p.message("Not enough space on inventory");
                    break;
                }
                pi.deleteItem(p, 995, pi.getItemSlot(p, 995), price);
                pi.addItem(p, item, 1);
            }
        }
        //sendShopItems(p);
        sendPlayerInventory(p);
    }

    public int findItemSlot(Player p, int item) {
        for (int i = 0; i < p.shopItems.length; i++)
            if (item == p.shopItems[i]) return i;
        return -1;
    }

    public int findFreeSlot(Player p) {
        for (int i = 0; i < p.shopItems.length; i++)
            if (p.shopItems[i] == -1) return i;
        return -1;
    }

    public void clearSlots(Player p) {
        for (int i = 0; i < p.shopItems.length; i++)
            if (p.shopItems[i] < 1) {
                p.shopItems[i] = -1;
            } else {
            }

    }

    public void sendShopItems(Player p) {
        p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
    }

    public boolean isItemOnShop(Player p, int item) {
        return findItemSlot(p, item) != -1;
    }

    public void sendPlayerInventory(Player p) {
        p.setItems(-1, 64209, 93, p.items, p.itemsN);

    }

    public int itemValue(int item) {
        return Engine.items.itemLists[item].shopValue;
    }

    public boolean itemStacks(int item) {
        return Engine.items.itemLists[item].itemStackable;
    }

    public void handleoption(Player p, int interfaceId, int buttonId, int buttonId2, int packetId) {
        switch (interfaceId) {
            case 620:
                /*if (buttonId == 25) {
                    p.getActionSender().setInterfaceConfig(p, 620, 23, false);
                    p.getActionSender().setInterfaceConfig(p, 620, 24, true);
                    p.getActionSender().setInterfaceConfig(p, 620, 29, false);
                    p.getActionSender().setInterfaceConfig(p, 620, 25, true);
                    p.getActionSender().setInterfaceConfig(p, 620, 27, true);
                    p.getActionSender().setInterfaceConfig(p, 620, 26, false);
                    p.getActionSender().setAccessMask(p, 1278, 23, 620, 0, 40);
                    mainstock = true;
                }
                if (buttonId == 26) {
                    p.getActionSender().setInterfaceConfig(p, 620, 23, true);
                    p.getActionSender().setInterfaceConfig(p, 620, 24, false);
                    p.getActionSender().setInterfaceConfig(p, 620, 29, true);
                    p.getActionSender().setInterfaceConfig(p, 620, 25, false);
                    p.getActionSender().setInterfaceConfig(p, 620, 27, false);
                    p.getActionSender().setInterfaceConfig(p, 620, 26, true);
                    p.getActionSender().setAccessMask(p, 1278, 24, 620, 0, 40);
                    mainstock = false;
                }*/
                PlayerItems pi = new PlayerItems();

                if (buttonId == 23) {
                    int itemid = returnItemId(shopid, buttonId2);
                    switch (packetId) {
                        case 233:
                            /* Value. */
                            p.message("This item costs " + Engine.items.getItemValue(itemid)
                                    + " coins.");
                            break;
                        case 21:
                            /* Buy 1. */
                            buystock(p, itemid, 1);
                            break;
                        case 169:
                            /* Buy 5. */
                            buystock(p, itemid, 5);
                            break;
                        case 214:
                            /* Buy 10. */
                            buystock(p, itemid, 10);
                            break;
                        case 173:
                            /* Buy 50. */
                            buystock(p, itemid, 50);
                            break;
                        case 232:
                            /*Examine. */
                            p.message(Engine.items.getItemDescription(itemid));
                            break;
                    }
                }
                if (buttonId == 24) {
                    int itemid = p.shopItems[buttonId2];


                    switch (packetId) {
                        case 233:
                            /* Value. */
                            p.message("This item costs " + Engine.items.getItemValue(itemid) + " coins.");
                            break;
                        case 21:
                            /* Buy 1. */
                            buy(p, itemid, 1);
                            break;
                        case 169:
                            /* Buy 5. */
                            buy(p, itemid, 5);
                            break;
                        case 214:
                            /* Buy 10. */
                            buy(p, itemid, 10);
                            break;
                        case 173:
                            /* Buy 50. */
                            buy(p, itemid, 50);
                            break;
                        case 90:
                            /*Examine. */
                            p.message(Engine.items.getItemDescription(itemid));
                            break;
                    }


                }
                break;
            case 621:
                if (buttonId == 0) {
                    int itemid = p.items[buttonId2];

                    switch (packetId) {
                        case 233:
                            /*Value.*/
                            p.message("This item is worth " + Engine.items.getItemValue(itemid) + " coins.");
                            break;
                        case 21:
                            /*
                            * Buy 1.
                            */
                            sell(p, itemid, 1);
                            break;
                        case 169:
                            /*
                            * Buy 5.
                            */
                            sell(p, itemid, 5);
                            break;
                        case 214:
                            /*
                            * Buy 10.
                            */

                            sell(p, itemid, 10);
                            break;
                        case 173:
                            /*
                            * Buy 50.
                            */
                            sell(p, itemid, 50);
                            break;
                        case 90:
                            /*
                            * Examine.
                            */
                            p.message(Engine.items.getItemDescription(itemid));
                            break;
                    }
                }
                break;
        }
    }

    public void shopopen(Player p, int shopId) {
        p.getActionSender().removeShownInterface(p);
        p.getActionSender().showInterface(p, 620); //Shop
        p.getActionSender().setInventory(p, 621);
        p.getActionSender().setTab(p, 80, 621);//Inv
        shopid = shopId;
        Object[] setshopparams = new Object[]{shopId, 93};

        int shi = 621 << 16;
        int ship = (620 << 16) + 24;

        Object[] invparams = new Object[]{"", "", "", "", "Sell 50", "Sell 10", "Sell 5", "Sell 1", "Value", -1, 0, 7, 4, 93, shi};
        Object[] shopparams = new Object[]{"", "", "", "", "Buy 50", "Buy 10", "Buy 5", "Buy 1", "Value", -1, 0, 4, 10, 31, ship};

        p.getActionSender().runScript(p, 25, setshopparams, "vg");//Loads main stock items
        p.getActionSender().runScript(p, 150, invparams, "IviiiIsssssssss");
        p.getActionSender().runScript(p, 150, shopparams, "IviiiIsssssssss");
        p.getActionSender().setAccessMask(p, 1278, 0, 621, 0, 28);

        if (mainstock) {
            p.getActionSender().setInterfaceConfig(p, 620, 23, false);
            p.getActionSender().setInterfaceConfig(p, 620, 24, true);
            p.getActionSender().setInterfaceConfig(p, 620, 29, false);
            p.getActionSender().setInterfaceConfig(p, 620, 25, true);
            p.getActionSender().setInterfaceConfig(p, 620, 27, true);
            p.getActionSender().setInterfaceConfig(p, 620, 26, false);
            p.getActionSender().setAccessMask(p, 1278, 23, 620, 0, 40);
        } else {
            p.getActionSender().setInterfaceConfig(p, 620, 23, true);
            p.getActionSender().setInterfaceConfig(p, 620, 24, false);
            p.getActionSender().setInterfaceConfig(p, 620, 29, true);
            p.getActionSender().setInterfaceConfig(p, 620, 25, false);
            p.getActionSender().setInterfaceConfig(p, 620, 27, false);
            p.getActionSender().setInterfaceConfig(p, 620, 26, true);
            p.getActionSender().setAccessMask(p, 1278, 24, 620, 0, 40);

        }
    }

    public int returnItemId(int shopid, int buttonId2) {
        switch (shopid) {
            case 868:
                switch (buttonId2) {
                    case 0:
                        return 1931;
                    case 1:
                        return 1935;
                    case 2:
                        return 1735;
                    case 3:
                        return 1925;
                    case 4:
                        return 1923;
                    case 5:
                        return 1887;
                    case 6:
                        return 590;
                    case 7:
                        return 1755;
                    case 8:
                        return 2347;
                    case 9:
                        return 550;
                    case 10:
                        return 9003;
                }
                break;
            case 2:
                switch (buttonId2) {
                    case 0:
                        return 1931;
                    case 1:
                        return 1935;
                    case 2:
                        return 1735;
                    case 3:
                        return 1925;
                    case 4:
                        return 1923;
                    case 5:
                        return 1887;
                    case 6:
                        return 590;
                    case 7:
                        return 1755;
                    case 8:
                        return 2347;
                    case 9:
                        return 550;
                    case 10:
                        return 9003;
                }
                break;
        }
        return -1;
    }

    public void openshop(Player p, int shopid) {
        buysBack = false;
        generalStore = false;
        switch (shopid) {
            case 1:
                shopopen(p, 868);
                generalStore = true;
                p.shopItems = p.shops.Generalshoplumb;
                p.shopItemsN = p.shops.GeneralshoplumbN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);//Shop Inventory
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);//Main Stock
                break;
            case 2:
                shopopen(p, 868);
                generalStore = true;
                p.shopItems = p.shops.Generalshopfalador;
                p.shopItemsN = p.shops.GeneralshopfaladorN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);//Shop Inventory
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);//Main Stock
                break;
            case 3: //magic shop
                shopopen(p, 700);
                generalStore = true;
                p.shopItems = p.shops.anothershop;
                p.shopItemsN = p.shops.anothershopN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);//Shop Inventory
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);//Main Stock           //set to 32 to be in playerstock
                break;
            case 4: // Shop Case
                shopopen(p, 730);
                p.shopItems = p.shops.CraftingShop; // Declaring this from Shops.java
                p.shopItemsN = p.shops.CraftingShopN; // Declaring this from Shops.java
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 5: // Shop Case
                shopopen(p, 701);
                p.shopItems = p.shops.anothershop3; // Declaring this from Shops.java
                p.shopItemsN = p.shops.anothershop3N; // Declaring this from Shops.java
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 6: // Shop Case
                shopopen(p, 702); // fighter shop
                p.shopItems = p.shops.anothershop2; // Declaring this from Shops.java
                p.shopItemsN = p.shops.anothershop2N; // Declaring this from Shops.java
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 8: // Pot/food shop
                shopopen(p, 704);
                p.shopItems = p.shops.foodpotshop;
                p.shopItemsN = p.shops.foodpotshopN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 9:
                shopopen(p, 868);
                generalStore = true;
                p.shopItems = p.shops.Generalshopvarrock;
                p.shopItemsN = p.shops.GeneralshopvarrockN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);//Shop Inventory
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);//Main Stock
                break;
            case 10: // Crafting shop
                shopopen(p, 868);
                generalStore = true;
                p.shopItems = p.shops.craftShop;
                p.shopItemsN = p.shops.craftShopN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);//Shop Inventory
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);//Main Stock
                break;
            case 11: // Runecrafting shop
                shopopen(p, 868);
                p.shopItems = p.shops.runecraftShop;
                p.shopItemsN = p.shops.runecraftShopN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 12: // Skillcapes shop
                shopopen(p, 868);
                p.shopItems = p.shops.skillcapeShop;
                p.shopItemsN = p.shops.skillcapeShopN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 13: // Skill shop
                buysBack = true;
                shopopen(p, 868);
                p.shopItems = p.shops.skillShop;
                p.shopItemsN = p.shops.skillShopN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 14: // Herb shop
                buysBack = true;
                shopopen(p, 868);
                p.shopItems = p.shops.herb1;
                p.shopItemsN = p.shops.herb1n;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 15: // Firemaking shop
                buysBack = true;
                shopopen(p, 868);
                p.shopItems = p.shops.fire1;
                p.shopItemsN = p.shops.fire1N;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 16: // smithing shop
                buysBack = true;
                shopopen(p, 868);
                p.shopItems = p.shops.smithing;
                p.shopItemsN = p.shops.smithingN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 17: // cooking shop
                shopopen(p, 868);
                p.shopItems = p.shops.cooking;
                p.shopItemsN = p.shops.cookingN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 18: // pure shop
                shopopen(p, 868);
                p.shopItems = p.shops.pureshop;
                p.shopItemsN = p.shops.pureshopN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 20: // donator shop
                shopopen(p, 868);
                p.shopItems = p.shops.donor;
                p.shopItemsN = p.shops.donorN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 21: // barrows donator shop
                shopopen(p, 868);
                p.shopItems = p.shops.donor1;
                p.shopItemsN = p.shops.donor1N;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
            case 22: //Skillcape Shop
                shopopen(p, 868);
                p.shopItems = p.shops.skillc;
                p.shopItemsN = p.shops.skillcN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
	    case 23: //rc robes shop
                shopopen(p, 868);
                p.shopItems = p.shops.rcRobes;
                p.shopItemsN = p.shops.rcRobesN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
	    case 24: //rc robes shop(WITH CAPE)
                shopopen(p, 868);
                p.shopItems = p.shops.rcRobesCape;
                p.shopItemsN = p.shops.rcRobesCapeN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
	    case 25: //Staff
                shopopen(p, 868);
                p.shopItems = p.shops.staffShop;
                p.shopItemsN = p.shops.staffShopN;
                p.setItems(-1, 64209, 93, p.items, p.itemsN);
                p.setItems(-1, 64271, 31, p.shopItems, p.shopItemsN);
                break;
        }

    }
}